package com.example.teaching_server.entity;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.PositiveOrZero;
import lombok.*;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;
import com.fasterxml.jackson.annotation.JsonFormat;
import java.time.LocalDateTime;

/**
 * 教室实体类
 */
@Getter
@Setter
@ToString
@EqualsAndHashCode(of = "id")
@Entity
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "classrooms", indexes = {
        @Index(name = "idx_room_number", columnList = "room_number"),
        @Index(name = "idx_building", columnList = "building")
})
public class ClassroomEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "classroom_id")
    private Long id;

    @Column(name = "room_number", nullable = false, unique = true)
    @NotBlank(message = "教室编号不能为空")
    private String roomNumber;

    @Column(nullable = false)
    @NotBlank(message = "教学楼不能为空")
    private String building;

    @Column(nullable = false)
    @PositiveOrZero(message = "教室容量必须大于等于0")
    @Builder.Default
    private Integer capacity = 0;

    // 保持原有的getter方法名称以兼容现有代码
    public Long getClassroomId() {
        return id;
    }

    public void setClassroomId(Long classroomId) {
        this.id = classroomId;
    }

    @Transient
    public String getFullRoomNumber() {
        return building + "-" + roomNumber;
    }

    @Transient
    public boolean canAccommodate(Integer studentCount) {
        return studentCount != null && capacity != null && studentCount <= capacity;
    }

    @PreUpdate
    private void validateCapacity() {
        if (capacity != null && capacity < 0) {
            throw new IllegalArgumentException("教室容量不能为负数");
        }
    }
}
